<?php

/*

***************************************
*                                     *
* Copyright 2011 David Matthew Pugh   *
* Contact Info:thebravedave@gmail.com *
*                                     *
***************************************

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

session_start();

require ('CreditCard.php');

//if the session variable total_after_shipping is not set, then redirect the page to cart.php  
 if(!isset($_SESSION['total_after_shipping']))
 {
             
             header("location:cart.php");
 } 
  


//if the session variable payment_first_name is not set then assign empty variables for use in html form to show the user what information they have inserted
if(!isset($_SESSION['payment_first_name'])) 
{
    $first_name= "";
    $last_name= "";
    $street_address= "";
    $city = "";
    $state= "" ;
    $email = "";
    $zip_code = "";
    $card_number =  "";
    $cvv = "";
    $cc_type = "";
    $expiration_month = "";
    $expiration_year = "Year";
    
}
//if payment_first_name is set then set all billing session information to variables for use in html forms to show the user what information they have inserted.
else
{
    $first_name= $_SESSION['payment_first_name'];
    $last_name= $_SESSION['payment_last_name'];
    $street_address= $_SESSION['payment_street_address'];
    $city = $_SESSION['payment_city'];
    $state= $_SESSION['payment_state'] ;
    $email = $_SESSION['payment_email'];
    $zip_code = $_SESSION['payment_zip_code'];
    $card_number =  $_SESSION['payment_card_number'];
    $cvv = $_SESSION['cvv'];
    $cc_type = $_SESSION['payment_cc_type'];
    $expiration_month = $_SESSION['payment_expiration_month'];
    
                                                 
//month info in database is set as a number, this procedure assigns the months name to corresponding numeric representation.    
    switch($expiration_month)
    {
      case 01;
        $expiration_month_name = "January";
        break;
      case 02;
       $expiration_month_name = "February";           
        break;  
      case 03;
        $expiration_month_name = "March";           
        break;
      case 04;
        $expiration_month_name = "April";           
        break;
      case 05;
       $expiration_month_name = "May";           
        break;
      case 06;
        $expiration_month_name = "June";           
        break;
      case 07;
        $expiration_month_name = "July";           
        break;
      case 08;
       $expiration_month_name = "August";           
        break;
      case 09;
        $expiration_month_name = "September";           
        break;
      case 10;
        $expiration_month_name = "October";           
        break;
      case 11; 
       $expiration_month_name = "November";           
        break;
      case 12; 
        $expiration_month_name = "December";           
        break;
     }
    
    $expiration_year = $_SESSION['payment_expiration_year'];
}
   
 // Include the header file:
$page_title = 'Step 2: Credit Card Information';
include('./includes/header.php');


//Define pages html/css info

// Check for form submission.
if (isset($_POST['submitted'])) 
{
        
    // Validate the credit card...

    // Check the expiration date:
    $year = (int) $_POST['cc_exp_year'];
    $month = (int) $_POST['cc_exp_month'];
    
    $array = file('./configurational/timezone.php');
    $timezonestring = "";
    foreach($array as $key => $value)
    {
        if(strpos($value, "timezone") !== false)
        {
            $timezonestring = $value;
        }
    }
    $zonearray = explode("=", $timezonestring);
    $zone = trim($zonearray[1]); 
    date_default_timezone_set($zone);
    // Get the current date:
    $current_year = date("Y");
    $current_month = date("n");
    
    // Validate the expiration date:
    if ( ($year > $current_year) OR  ($year == $current_year) AND ($month >= $current_month) )
    {   
        
       
        // Create the object:
        $cc = new Validate_Finance_CreditCard();    

        // Validate the card number and type:
        if ($cc->number($_POST['card_number'], $_POST['cc_type'])) 
        {
        
            
            //assign the form submission to local variables
            $first_name = $_POST['first_name'];
            $last_name = $_POST['last_name'];
            $street_address = $_POST['street_address'];
            $city = $_POST['city'];
            $state = $_POST['state'];
            $email = $_POST['email'];
            $zip_code = $_POST['zip_code'];
            $card_number = $_POST['card_number'];         
            $cvv = $_POST['cvv']; 
            $cvv = (int)$cvv;    
            $cc_type = $_POST['cc_type'];
            $expiration_month = $_POST['cc_exp_month'];
            $expiration_year = $_POST['cc_exp_year'];                          
            //assign the local variables  from the the form submission variables to session variables
            $_SESSION['payment_first_name'] = $first_name;
            $_SESSION['payment_last_name'] = $last_name;   
            $_SESSION['payment_street_address'] = $street_address;
            $_SESSION['payment_city'] = $city;
            $_SESSION['payment_state'] = $state; 
            $_SESSION['payment_email'] = $email;
            $_SESSION['payment_zip_code'] = $zip_code;
            $_SESSION['payment_card_number'] = $card_number;
            $_SESSION['cvv'] = $cvv; 
            $_SESSION['payment_cc_type'] = $cc_type;
            $_SESSION['payment_expiration_month'] = $expiration_month;
            $_SESSION['payment_expiration_year'] = $expiration_year; 
           
            $submitted = $_POST['submitted']; 
       

            if(isset($_POST['submitted']))  
            {
                header("location:finalize.php");
            }                  
        } 
        else // Invalid card number or type.     
        { 
            echo '<p class="error">Please enter a valid credit card number and type.</p>';
        }

    } 
    else 
    { // Invalid date.
        echo '<p class="error">Please enter a valid expiration date.</p>';
    }

}
 ?>
 
 

<form action="payment.php" method="POST" name="payment">
 
<table border="0" width="90%" cellspacing="2" cellpadding="2" align="center">                                          
    
    <tr>
        <td align="right">First Name:</td>  
        <td align="left"><input type="text" name="first_name" id="first_name" value="<?php echo $first_name;?>"></td>
    </tr>
                 
    <tr>
        <td align="right">Last Name:</td>
        <td align="left"><input type="text" name="last_name" id="last_name" value="<?php echo $last_name;?>"></td>
    </tr>  
    
    <tr>
        <td align="right">Street Address:</td>
        <td align="left"><input type="text" name="street_address" id="street_address" value="<?php echo $street_address;?>"></td>
    </tr>
    
    <tr>
        <td align="right">City:</td>
        <td align="left"><input type="text" name="city" id="city" value="<?php echo $city;?>"></td>
    </tr>
    
    <tr>
        <td align="right">State:</td>
        <td align="left"><select name="state">
        <?php
        if(isset($_SESSION['payment_state']))
        {
            echo ' <option value="' . $state . '">' . $state . '</option>"' ; 
        }
       
        ?>
                           <option value="AL">Alabama</option>
                            <option value="AK">Alaska</option>
                            <option value="AZ">Arizona</option>
                            <option value="AR">Arkansas</option>
                            <option value="CA">California</option>
                            <option value="CO">Colorado</option>
                            <option value="CT">Connecticut</option>
                            <option value="DE">Delaware</option>
                            <option value="DC">Dist of Columbia</option>
                            <option value="FL">Florida</option>
                            <option value="GA">Georgia</option>
                            <option value="HI">Hawaii</option>
                            <option value="ID">Idaho</option>
                            <option value="IL">Illinois</option>
                            <option value="IN">Indiana</option>
                            <option value="IA">Iowa</option>
                            <option value="KS">Kansas</option>
                            <option value="KY">Kentucky</option>
                            <option value="LA">Louisiana</option>
                            <option value="ME">Maine</option>
                            <option value="MD">Maryland</option>
                            <option value="MA">Massachusetts</option>
                            <option value="MI">Michigan</option>
                            <option value="MN">Minnesota</option>
                            <option value="MS">Mississippi</option>
                            <option value="MO">Missouri</option>
                            <option value="MT">Montana</option>
                            <option value="NE">Nebraska</option>
                            <option value="NV">Nevada</option>
                            <option value="NH">New Hampshire</option>
                            <option value="NJ">New Jersey</option>
                            <option value="NM">New Mexico</option>
                            <option value="NY">New York</option>
                            <option value="NC">North Carolina</option>
                            <option value="ND">North Dakota</option>
                            <option value="OH">Ohio</option>
                            <option value="OK">Oklahoma</option>
                            <option value="OR">Oregon</option>
                            <option value="PA">Pennsylvania</option>
                            <option value="RI">Rhode Island</option>
                            <option value="SC">South Carolina</option>
                            <option value="SD">South Dakota</option>
                            <option value="TN">Tennessee</option>
                            <option value="TX">Texas</option>
                            <option value="UT">Utah</option>
                            <option value="VT">Vermont</option>
                            <option value="VA">Virginia</option>
                            <option value="WA">Washington</option>
                            <option value="WV">West Virginia</option>
                            <option value="WI">Wisconsin</option>
                            <option value="WY">Wyoming</option>
                            </select>
                       </td>   
    </tr>
    
    <tr>
        <td align="right">Zip Code:</td>
        <td align="left"><input type="text" name="zip_code" id="zip_code" value="<?php echo $zip_code;?>"></td>
    </tr>
    
    <tr>
        <td align="right">Email Address:</td>
        <td align="left"><input type="text" name="email" id="email" value="<?php echo $email;?>"></td>
    </tr>
    
    <tr>
        <td align="right">Credit Card Type:</td>
        <td align="left"><select name="cc_type"> 
        <option value="visa">Visa</option> 
        <option value="amex">American Express</option>
        <option value="mastercard">MasterCard</option>
        </select></td>
    </tr>
        
    <tr> 
        <td align="right">Credit Card Number:</td>
        <td align="left"><input type="text" name="card_number" maxlength="16" value="<?php echo $card_number;?>"/></td>
    </tr>
    
    <tr> 
        <td align="right">CVV:</td>
    <td align="left"><input type="text" name="cvv" maxlength="6" value="<?php echo $cvv;?>"/></td>
    </tr>
    
    <tr> 
        <td align="right">Expiration Date:</td>
        <td align="left">
        <select name="cc_exp_month">
        <?php 
        if(isset($_SESSION['payment_expiration_month']))
        {
        ?>
            <option value="<?php echo $expiration_month ?>"><?php echo $expiration_month_name ?></option>
        <?php 
        }
        else
        {?>
            <option value="">Month</option>
            <?php 
        }
        ?>
        <option value="01">Jan</option>
        <option value="02">Feb</option>
        <option value="03">Mar</option>
        <option value="04">Apr</option>
        <option value="05">May</option>
        <option value="06">Jun</option>
        <option value="07">Jul</option>
        <option value="08">Aug</option>
        <option value="09">Sep</option>
        <option value="10">Oct</option>
        <option value="11">Nov</option>
        <option value="12">Dec</option>
        </select> 
        <select name="cc_exp_year">
        <option value="<?php echo $expiration_year ?>"><?php echo $expiration_year ?></option>
        
        <?php for ($start = date('Y'), $end = date('Y') + 10; $start < $end; $start++) 
        {
            echo "<option value=\"$start\">$start</option>\n";
        }    
        ?>
        </select></td>
    </tr>
    </table>
    <br />
    
   
    <table align="center">
    <tr align="center">
          
        <td align="center"><input type="submit" name="submitted" value="Step 3: Confirm Order"></td>
         
        </form>
    </tr>   
</table>



